package com.inmaytide.website.controller.deploy;

import com.inmaytide.website.domain.entities.deploy.DeployProject;
import com.inmaytide.website.facade.deploy.DeployProjectFacade;
import com.inmaytide.website.framework.bean.JsonResult;
import com.inmaytide.website.utils.log.annotation.LogInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Controller
@RequestMapping("deploy/project")
public class DeployProjectController {

    @Autowired
    private DeployProjectFacade deployProjectFacade;

    @LogInformation(description = "部署项目列表页")
    @RequestMapping(value = "list")
    public String list(ModelMap model) {
        List<DeployProject> projectList = deployProjectFacade.findList();
        model.put("projectList", projectList);
        return "deploy/project/list";
    }

    @LogInformation(description = "加载项目")
    @RequestMapping(value = "load")
    public String load(Long id, ModelMap model) {
        DeployProject project = null;
        if (id != null) {
            project = deployProjectFacade.findOne(id);
        } else {
            project = new DeployProject();
            project.setSort(deployProjectFacade.getMaxSort() + 1);
        }
        model.put("project", project);
        return "deploy/project/edit";
    }

    @LogInformation(description = "保存项目")
    @RequestMapping(value = "save")
    public JsonResult save(DeployProject project) {
        return deployProjectFacade.save(project);
    }


    @LogInformation(description = "删除项目")
    @RequestMapping(value = "delete/{id}")
    public JsonResult delete(@PathVariable Long id) {
        return deployProjectFacade.delete(id);
    }

    /**
     * BootStrapValidator校验字段
     *
     * @param path 路径
     * @return 格式要求：{"valid" : true}
     */
    @LogInformation(description = "检查项目路径")
    @RequestMapping(value = "checkPath")
    public JsonResult checkPath(@RequestParam String path) {
        return new JsonResult().setProperty("valid", deployProjectFacade.validDir(path));
    }

}
